<template>
  <div class="main-border">
    <el-menu default-active="1" class="el-menu-demo" mode="horizontal" @select="handleSelect">
      <el-menu-item index="1">上线的文章</el-menu-item>
      <el-menu-item index="2">下架的文章</el-menu-item>
      <el-button type="text" @click="openAdd()">发布文章</el-button>
    </el-menu>
    <el-table v-if="this.mode == 1"
              :data="onlineGoods"
              stripe
              style="width: 100%;color: #5a5c61;">
      <el-table-column
          prop="releaseTime"
          label="发布日期"
          width="200">
      </el-table-column>
      <el-table-column
          prop="teaName"
          label="文章名称"
          show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
          prop="user.nickname"
          label="发布用户"
          show-overflow-tooltip
          min-width="100"
          width="100">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="danger"
              @click="openEdit(scope.$index)">修改
          </el-button>
          <el-button
              size="mini"
              type="danger"
              @click="makeOfflineGoods(scope.$index)">违规下架
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-table v-show="this.mode == 2"
              :data="OfflineGoods"
              stripe
              style="width: 100%;color: #5a5c61;">
      <el-table-column
          prop="releaseTime"
          label="发布日期"
          width="200">
      </el-table-column>
      <el-table-column
          prop="teaName"
          label="文章名称"
          show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
          prop="user.nickname"
          label="发布用户"
          show-overflow-tooltip
          min-width="100"
          width="100">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="danger"
              @click="resUpGoods(scope.$index)">重新上架
          </el-button>
          <el-button
              size="mini"
              type="danger"
              @click="deleteGoods(scope.$index)">永久删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
          @current-change="handleCurrentChange"
          :current-page.sync="nowPage"
          :page-size="8"
          background
          layout="prev, pager, next,jumper"
          :total="total">
      </el-pagination>
    </div>

    <!--  新增文章弹窗  -->
    <el-dialog
        title="发布文章"
        :visible.sync="dialogVisible"
        width="60%">
      <el-form class="release-tea-container-form">
        <div class="release-tea-container-form">
          <div class="release-tip">文章名称</div>
          <el-input placeholder="请输入文章名称" v-model="teaItemInfo.teaName"
                    maxlength="30"
                    show-word-limit>
          </el-input>
          <div style="margin: 10px 0">文章内容</div>
          <div>
            <Editor id="tinymce" v-model="teaItemInfo.teaDetails" :init="init"></Editor>
          </div>
        </div>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleClose">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import options from "@/components/common/country-data";

export default {
  name: "articleList",
  data() {
    return {
      mode: 1,
      nowPage: 1,
      total: 0,
      onlineGoods: [],
      OfflineGoods: [],
      dialogVisible: false,
      selectedOptions: [],
      options: options,
      teaItemInfo: {
        teaName: '',
        teaDetails: '',
        pictureList: '',
        teaPrice: 0,
        teaPlace: '',
        teaLabel: '12'
      },
      videoUrl: '',
      value: '',
      contenyt: {},
      init: {
        language_url: "/zh_CN.js", // 语言包位置，因为放在public下所以可以省略public
        selector: "#tinymce", //tinymce的id
        language: "zh_CN", //语言类型
        skin_url: "/skins/ui/oxide",
        height: 500, //编辑器高度
        browser_spellcheck: true, // 拼写检查
        paste_data_images: true, // 允许粘贴图像
        plugins: "image link code table lists wordcount",
        branding: false, //是否禁用“Powered by TinyMCE”
        toolbar: "fontselect fontsizeselect link lineheight forecolor backcolor bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | image quicklink h2 h3 blockquote table numlist bullist preview fullscreen", //工具栏
        fontsize_formats: "12px 14px 16px 18px 24px 36px 48px 56px 72px",
        font_formats: "微软雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;苹果苹方=PingFang SC,Microsoft YaHei,sans-serif;宋体=simsun,serif;仿宋体=FangSong,serif;黑体=SimHei,sans-serif;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;",
      }
    }
  },
  created() {
    this.getOnlineGoods();
  },
  methods: {
    handleCurrentChange(val) {
      this.nowPage = val;
      if (this.mode == 1) {
        this.getOnlineGoods();
      }
      if (this.mode == 2) {
        this.getOfflineGoods();
      }
    },
    handleSelect(val) {
      if (this.mode !== val) {
        this.mode = val;
        if (val == 1) {
          this.nowPage = 1;
          this.getOnlineGoods();
        }
        if (val == 2) {
          this.nowPage = 1;
          this.getOfflineGoods();
        }
      }
    },
    makeOfflineGoods(i) {
      this.$api.updateGoods({
        id: this.onlineGoods[i].id,
        status: 2
      }).then(res => {
        if (res.status_code == 1) {
          this.getOnlineGoods();
        } else {
          this.$message.error(res.msg)
        }
      }).catch(e => {
        console.log(e)
      })
    },
    deleteGoods(i) {
      this.$api.updateGoods({
        id: this.OfflineGoods[i].id,
        status: 0
      }).then(res => {
        if (res.status_code == 1) {
          this.getOfflineGoods();
        } else {
          this.$message.error(res.msg)
        }

      }).catch(e => {
        console.log(e)
      })
    },
    // 重新上架
    resUpGoods(i) {
      this.$api.updateGoods({
        id: this.OfflineGoods[i].id,
        status: 1
      }).then(res => {
        if (res.status_code == 1) {
          this.getOfflineGoods();
        } else {
          this.$message.error(res.msg)
        }

      }).catch(e => {
        console.log(e)
      })
    },
    getOnlineGoods() {
      this.$api.getArticle({
        status: 1,
        teaLabel: 12,
        page: this.nowPage,
        nums: 8
      }).then(res => {
        console.log(res)
        if (res.status_code == 1) {
          this.onlineGoods = res.data.list;
          this.total = res.data.count;
        } else {
          this.$message.error(res.msg)
        }
      }).catch(e => {
        console.log(e)
      })
    },
    getOfflineGoods() {
      this.$api.getArticle({
        status: 2,
        teaLabel: 12,
        page: this.nowPage,
        nums: 8
      }).then(res => {
        if (res.status_code == 1) {
          this.OfflineGoods = res.data.list;
          this.total = res.data.count;
        } else {
          this.$message.error(res.msg)
        }
      }).catch(e => {
        console.log(e)
      })
    },
    handleClose() {
      console.log(this.teaItemInfo);
      if (this.teaItemInfo.teaName &&
          this.teaItemInfo.teaDetails &&
          this.teaItemInfo.teaLabel ) {
        if (!this.teaItemInfo.id){
          this.$api.addteaItem(this.teaItemInfo).then(res => {
            if (res.status_code === 1) {
              this.$message({
                message: '发布成功！',
                type: 'success'
              });
              console.log(res.data);
              this.dialogVisible = false;
              this.teaItemInfo = {
                teaName: '',
                teaDetails: '',
                pictureList: '',
                teaPrice: 0,
                teaPlace: '',
                teaLabel: '12'
              }
              this.imgList = []
              this.selectedOptions = []
              this.options2 = []
              this.dialogImageUrl = ''
              this.getOnlineGoods()
              // this.$router.replace({path: '/details', query: {id: res.data.id}});
            } else {
              this.$message.error('发布失败！' + res.msg);
            }
          }).catch(e => {
            this.$message.error('请填写完整信息');
          })
        }else {
          this.$api.updateteaItem(this.teaItemInfo).then(res => {
            if (res.status_code === 1) {
              this.$message({
                message: '修改成功！',
                type: 'success'
              });
              console.log(res.data);
              this.dialogVisible = false;
              this.teaItemInfo = {
                teaName: '',
                teaDetails: '',
                pictureList: '',
                teaPrice: 0,
                teaPlace: '',
                teaLabel: ''
              }
              this.getOnlineGoods()
            } else {
              this.$message.error('修改失败！' + res.msg);
            }
          })
        }
      } else {
        this.$message.error('请填写完整信息！');
      }
    },
    openAdd() {
      this.dialogVisible = true;
      this.title = "发布物品";
      this.teaItemInfo = {
        teaDetails: '',
      };
    },
    //   打开修改窗口
    openEdit(i) {
      this.dialogVisible = true;
      this.title = "修改物品";
      this.$api.getteaItem({
        id: this.onlineGoods[i].id,
      }).then(res => {
        console.log(res.data);
        this.teaItemInfo = res.data;
        console.log(this.dialogImageUrl)
      })
    }
  },
  mounted() {
    tinymce.init({});
  }

}
</script>

<style scoped>

.main-border {
  background-color: #FFF;
  padding: 10px 30px;
  box-shadow: 0 1px 15px -6px rgba(0, 0, 0, .5);
  border-radius: 5px;
}

.block {
  display: flex;
  justify-content: center;
  padding-top: 15px;
  padding-bottom: 10px;
  width: 100%;
}

.release-tea-place {
  margin-bottom: 15px;
}

.release-tip {
  color: #555555;
  float: left;
  padding-right: 5px;
  height: 36px;
  line-height: 36px;
  font-size: 14px;
}

.release-tea-container-picture {
  width: 500px;
  height: 400px;
  margin: 20px 0;

}
.release-tea-container-picture-title {
  margin: 10px 0;
  color: #555555;
  font-size: 14px;
}

.picture-list {
  margin: 20px 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100px;
}
</style>